/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.dess.dislock.model;

import com.hyts.dess.base.error.DessException;
import lombok.Data;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @project-name:dess
 * @package-name:com.hyts.dess.dislock.model
 * @author:LiBo/Alex
 * @create-date:2020-08-03 15:20
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description: redis连接对象封装类
 *
 *  Config config = new Config();
 * config.useClusterServers()
 *     .setScanInterval(2000) // cluster state scan interval in milliseconds
 *     .addNodeAddress("redis://127.0.0.1:7000", "redis://127.0.0.1:7001")
 *     .addNodeAddress("redis://127.0.0.1:7002");
 * // 配置redis地址
 * RedissonClient redisson = Redisson.create(config);
 * // 获取实例
 * RLock lock = redisson.getLock("anyLock");
 * // 获取锁
 * lock.lock();
 * try {
 *     // 执行业务代码块
 *     ...
 * } finally {
 *     // 释放锁
 *     lock.unlock();
 * }
 *
 *
 *
 */
@Data
public class RedisConnectionWrapper {
    /**
     * 连接数据字符串
     */
    private List<String> connectionUrls;

    /**
     * 集群状态扫描的时间间隔
     */
    private int clusterScanIntervalTime = 5000;

    /**
     * 是否采用集群模式
     */
    private boolean useClusterModel = false;

    /**
     * 构建redis配置类对象操作
     * @return
     */
    public Config buildRedisConfig(){
        if(CollectionUtils.isEmpty(connectionUrls)){
            throw new DessException("无法获取到相关连接url对象操作服务处理功能");
        }
        Config config = new Config();
        if(useClusterModel){
            ClusterServersConfig clusterServersConfig = config.useClusterServers();
            clusterServersConfig.setScanInterval(clusterScanIntervalTime);
        }else{
            SingleServerConfig singleServerConfig = config.useSingleServer();
        }
        return null;
    }

}
